package ai.george.ai.controller.openai.chat;


import org.springframework.ai.chat.messages.SystemMessage;
import org.springframework.ai.chat.messages.UserMessage;
import org.springframework.ai.chat.model.ChatResponse;
import org.springframework.ai.chat.prompt.Prompt;
import org.springframework.ai.openai.OpenAiChatModel;
import org.springframework.ai.openai.OpenAiChatOptions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

@RestController
@RequestMapping("/chat-model")
public class ChatModelController {


    @Autowired
    private OpenAiChatModel openAiChatModel;


    @GetMapping("/text")
    public String text(@RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") String query) {
        return openAiChatModel.call(query);
    }


    @GetMapping("/messages")
    public String message(@RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") String query) {
        SystemMessage systemMessage = new SystemMessage("你一个博学的智能聊天助手，请根据用户提问回答！");
        UserMessage userMessage = new UserMessage(query);
        return openAiChatModel.call(systemMessage, userMessage);
    }


    @GetMapping("/prompt")
    public ChatResponse prompt(@RequestParam(value = "model", defaultValue = "qwen-flash") String model,
                               @RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") String query) {
        SystemMessage systemMessage = new SystemMessage("你一个博學的智能聊天助手，请根据用户提问回答！");
        UserMessage userMessage = new UserMessage(query);
        OpenAiChatOptions chatOptions = OpenAiChatOptions.builder()
                .model(model)
                .temperature(0.7)
                .build();
        Prompt prompt = new Prompt(List.of(systemMessage, userMessage), chatOptions);
        ChatResponse response = openAiChatModel.call(prompt);
        return response;
    }


    @GetMapping("/text-stream")
    public Flux<String> textStream(@RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") String query) {
        return openAiChatModel.stream(query);
    }






    @GetMapping("/prompt-stream")
    public Flux<ChatResponse> promptStream(@RequestParam(value = "model", defaultValue = "qwen-flash") String model,
                               @RequestParam(value = "query", defaultValue = "你好，很高兴认识你，能简单介绍一下自己吗？") String query) {
        SystemMessage systemMessage = new SystemMessage("你一个博學的智能聊天助手，请根据用户提问回答！");
        UserMessage userMessage = new UserMessage(query);
        OpenAiChatOptions chatOptions = OpenAiChatOptions.builder()
                .model(model)
                .temperature(0.7)
                .build();
        Prompt prompt = new Prompt(List.of(systemMessage, userMessage), chatOptions);
        Flux<ChatResponse> response = openAiChatModel.stream(prompt);
        return response;
    }

}
